home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Color Picker SDK / Sample Code / Picker Demo Sample / TestPickerInterface.c < prev    next >
Encoding:
Text File  |  1997-06-13  |  11.7 KB  |  553 lines  |  [TEXT/CWIE]

  1. //===============================================================================
  2. //===============================================================================
  3. //
  4. //                                TestPickerInterface.c
  5. //
  6. //                                  john calhoun 1997
  7. //
  8. //===============================================================================
  9. //===============================================================================
  10.  
  11.  
  12. #include "TestPicker.h"
  13. #include "GetMonitorProfile.h"
  14.  
  15.  
  16. Rect        bounceRect;
  17. GWorldPtr    theGWorld;
  18. UInt32        wasTicks;
  19. SInt16        x1, y1, x2, y2, deltaX1, deltaX2, deltaY1, deltaY2;
  20.  
  21.  
  22. OSErr GetProfileRef (CMProfileRef *);
  23.  
  24.  
  25. //===================================================================== Functions
  26. //--------------------------------------------------------------------- UpdateVennColors
  27.  
  28. void UpdateVennColors (void)
  29. {
  30.     RGBColor    theColor;
  31.     
  32.     theColor.red = theRGBColor.red;
  33.     theColor.green = 0;
  34.     theColor.blue = 0;
  35.     RGBForeColor(&theColor);
  36.     PaintRgn(regionR);
  37.     
  38.     theColor.red = 0;
  39.     theColor.green = theRGBColor.green;
  40.     theColor.blue = 0;
  41.     RGBForeColor(&theColor);
  42.     PaintRgn(regionG);
  43.     
  44.     theColor.red = 0;
  45.     theColor.green = 0;
  46.     theColor.blue = theRGBColor.blue;
  47.     RGBForeColor(&theColor);
  48.     PaintRgn(regionB);
  49.     
  50.     theColor.red = theRGBColor.red;
  51.     theColor.green = theRGBColor.green;
  52.     theColor.blue = 0;
  53.     RGBForeColor(&theColor);
  54.     PaintRgn(regionRG);
  55.     
  56.     theColor.red = 0;
  57.     theColor.green = theRGBColor.green;
  58.     theColor.blue = theRGBColor.blue;
  59.     RGBForeColor(&theColor);
  60.     PaintRgn(regionGB);
  61.     
  62.     theColor.red = theRGBColor.red;
  63.     theColor.green = 0;
  64.     theColor.blue = theRGBColor.blue;
  65.     RGBForeColor(&theColor);
  66.     PaintRgn(regionBR);
  67.     
  68.     theColor.red = theRGBColor.red;
  69.     theColor.green = theRGBColor.green;
  70.     theColor.blue = theRGBColor.blue;
  71.     RGBForeColor(&theColor);
  72.     PaintRgn(regionRGB);
  73.     
  74.     ForeColor(blackColor);
  75. }
  76.  
  77. //--------------------------------------------------------------------- UpdateCMYKColors
  78.  
  79. void UpdateCMYKColors (void)
  80. {
  81.     CMYColor    theCMYColor;
  82.     RGBColor    theColor;
  83.     Str255        tempStr;
  84.     GrafPtr        wasPort;
  85.     
  86.     GetPort(&wasPort);
  87.     SetPort(simpleWindow);
  88.     
  89.     theCMYColor.cyan = cmykColor.cmyk.cyan;
  90.     theCMYColor.magenta = 0;
  91.     theCMYColor.yellow = 0;
  92.     CMY2RGB(&theCMYColor, &theColor);
  93.     RGBForeColor(&theColor);
  94.     PaintRect(&rectC);
  95.     
  96.     theCMYColor.cyan = 0;
  97.     theCMYColor.magenta = cmykColor.cmyk.magenta;
  98.     theCMYColor.yellow = 0;
  99.     CMY2RGB(&theCMYColor, &theColor);
  100.     RGBForeColor(&theColor);
  101.     PaintRect(&rectM);
  102.     
  103.     theCMYColor.cyan = 0;
  104.     theCMYColor.magenta = 0;
  105.     theCMYColor.yellow = cmykColor.cmyk.yellow;
  106.     CMY2RGB(&theCMYColor, &theColor);
  107.     RGBForeColor(&theColor);
  108.     PaintRect(&rectY);
  109.     
  110.     theColor.red = 65535 - cmykColor.cmyk.black;
  111.     theColor.green = 65535 - cmykColor.cmyk.black;
  112.     theColor.blue = 65535 - cmykColor.cmyk.black;
  113.     RGBForeColor(&theColor);
  114.     PaintRect(&rectK);
  115.     ForeColor(blackColor);
  116.     
  117.                 // Draw CMYK percentages.    
  118.     NumToString(((long)cmykColor.cmyk.cyan * 100L + 328) / 65535L, tempStr);
  119.     PasStringConcat(tempStr, "\p%");
  120.     MoveTo(rectC.left + 17, rectC.top + 20);
  121.     DrawString(tempStr);
  122.     
  123.     NumToString(((long)cmykColor.cmyk.magenta * 100L + 328) / 65535L, tempStr);
  124.     PasStringConcat(tempStr, "\p%");
  125.     MoveTo(rectM.left + 17, rectM.top + 20);
  126.     DrawString(tempStr);
  127.     
  128.     NumToString(((long)cmykColor.cmyk.yellow * 100L + 328) / 65535L, tempStr);
  129.     PasStringConcat(tempStr, "\p%");
  130.     MoveTo(rectY.left + 17, rectY.top + 20);
  131.     DrawString(tempStr);
  132.     
  133.     if (cmykColor.cmyk.black < 32768)
  134.         ForeColor(blackColor);
  135.     else
  136.         ForeColor(whiteColor);
  137.     NumToString(((long)cmykColor.cmyk.black * 100L + 328) / 65535L, tempStr);
  138.     PasStringConcat(tempStr, "\p%");
  139.     MoveTo(rectK.left + 17, rectK.top + 20);
  140.     DrawString(tempStr);
  141.     ForeColor(blackColor);
  142.     
  143.     SetPort(wasPort);
  144. }
  145.  
  146. //--------------------------------------------------------------------- UpdateBouncingLine
  147.  
  148. void UpdateBouncingLine (void)
  149. {
  150.     UInt32        tickDiff;
  151.     GDHandle    wasWorld;
  152.     CGrafPtr    wasCPort;
  153.     
  154.     tickDiff = (TickCount() - wasTicks) / 2;
  155.     
  156.     PenSize(kLineThick, kLineThick);
  157.     RGBForeColor(&theRGBColor);
  158.     
  159.     if (!theGWorld)
  160.         goto skipGWorldSetup;
  161.     
  162.     GetGWorld(&wasCPort, &wasWorld);
  163.     LockPixels(GetGWorldPixMap(theGWorld));
  164.     SetGWorld(theGWorld, 0L);
  165.     RGBForeColor(&theRGBColor);
  166.     SetGWorld(wasCPort, wasWorld);
  167.     
  168. skipGWorldSetup:
  169.     
  170.     while (tickDiff > 0)
  171.     {
  172.         x1 += deltaX1;
  173.         y1 += deltaY1;
  174.         if (x1 > bounceRect.right)
  175.         {
  176.             deltaX1 = -deltaX1;
  177.             x1 = bounceRect.right;
  178.         }
  179.         else if (x1 < bounceRect.left)
  180.         {
  181.             deltaX1 = -deltaX1;
  182.             x1 = bounceRect.left;
  183.         }
  184.         if (y1 > bounceRect.bottom)
  185.         {
  186.             deltaY1 = -deltaY1;
  187.             y1 = bounceRect.bottom;
  188.         }
  189.         else if (y1 < bounceRect.top)
  190.         {
  191.             deltaY1 = -deltaY1;
  192.             y1 = bounceRect.top;
  193.         }
  194.         
  195.         x2 += deltaX2;
  196.         y2 += deltaY2;
  197.         if (x2 > bounceRect.right)
  198.         {
  199.             deltaX2 = -deltaX2;
  200.             x2 = bounceRect.right;
  201.         }
  202.         else if (x2 < bounceRect.left)
  203.         {
  204.             deltaX2 = -deltaX2;
  205.             x2 = bounceRect.left;
  206.         }
  207.         if (y2 > bounceRect.bottom)
  208.         {
  209.             deltaY2 = -deltaY2;
  210.             y2 = bounceRect.bottom;
  211.         }
  212.         else if (y2 < bounceRect.top)
  213.         {
  214.             deltaY2 = -deltaY2;
  215.             y2 = bounceRect.top;
  216.         }
  217.         
  218.         MoveTo(x1 >> 2, y1 >> 2);
  219.         LineTo(x2 >> 2, y2 >> 2);
  220.         
  221.         if (!theGWorld)
  222.             goto skipGWorldDraw;
  223.         
  224.         SetGWorld(theGWorld, 0L);
  225.         MoveTo((x1 >> 2) - (76 >> 2), (y1 >> 2) - (240 >> 2));
  226.         LineTo((x2 >> 2) - (76 >> 2), (y2 >> 2) - (240 >> 2));
  227.         SetGWorld(wasCPort, wasWorld);
  228.         
  229. skipGWorldDraw:
  230.         
  231.         tickDiff--;
  232.         wasTicks = TickCount();
  233.     }
  234.     
  235.     if (theGWorld)
  236.         UnlockPixels(GetGWorldPixMap(theGWorld));
  237.     PenSize(1, 1);
  238.     ForeColor(blackColor);
  239. }
  240.  
  241. //--------------------------------------------------------------------- UpdateMainWindowText
  242.  
  243. void UpdateMainWindowText (void)
  244. {
  245.     Rect        destRect, srcRect;
  246.     PicHandle    thePict;
  247.     Str255        messageStr, tempStr;
  248.     long        value;
  249.     OSErr        theErr;
  250.     
  251.     theErr = Gestalt(gestaltColorPickerVersion, &value);
  252.     if (theErr)
  253.         PasStringCopy("\pOriginal Color Picker.", messageStr);
  254.     else
  255.     {
  256.         PasStringCopy("\pColor Picker version is: ", messageStr);
  257.         NumToString((value / 256) / 16, tempStr);
  258.         PasStringConcat(messageStr, tempStr);
  259.         NumToString((value / 256) % 16, tempStr);
  260.         PasStringConcat(messageStr, tempStr);
  261.         PasStringConcat(messageStr, "\p.");
  262.         NumToString((value % 256) / 16, tempStr);
  263.         PasStringConcat(messageStr, tempStr);
  264.         NumToString((value % 256) % 16, tempStr);
  265.         PasStringConcat(messageStr, tempStr);
  266.     }
  267.     
  268.     TextFace(bold);
  269.     MoveTo(20, 16);
  270.     DrawString(messageStr);
  271.     
  272.                 // Draw text around venn diagram.
  273.     PaintRgn(blackRgn);
  274.     ForeColor(whiteColor);
  275.     MoveTo(78, 258);
  276.     DrawString("\pRed");
  277.     MoveTo(35, 390);
  278.     DrawString("\pGreen");
  279.     MoveTo(120, 390);
  280.     DrawString("\pBlue");
  281.     ForeColor(blackColor);
  282.     
  283.                 // Draw CMYK Rects.
  284.     MoveTo(rectC.left + 13, rectC.top - 4);
  285.     DrawString("\pCyan");
  286.     MoveTo(rectM.left + 1, rectM.top - 4);
  287.     DrawString("\pMagenta");
  288.     MoveTo(rectY.left + 6, rectY.top - 4);
  289.     DrawString("\pYellow");
  290.     MoveTo(rectK.left + 10, rectK.top - 4);
  291.     DrawString("\pBlack");
  292.     
  293.                 // Draw PICT.
  294.     SetRect(&destRect, 0, 0, 416, 312);
  295.     OffsetRect(&destRect, windowBounds.right - (destRect.right + 8), 
  296.             windowBounds.bottom - (destRect.bottom + 8));
  297.     thePict = GetPicture(129);
  298.     if (thePict)
  299.     {
  300.         DrawPicture(thePict, &destRect);
  301.         ReleaseResource((Handle)thePict);
  302.     }
  303.     
  304.                 // Draw bouncing line box.
  305.     destRect.left = bounceRect.left >> 2;
  306.     destRect.top = bounceRect.top >> 2;
  307.     destRect.right = (bounceRect.right >> 2) + kLineThick;
  308.     destRect.bottom = (bounceRect.bottom >> 2) + kLineThick;
  309.     srcRect = destRect;
  310.     OffsetRect(&srcRect, -srcRect.left, -srcRect.top);
  311.     if (theGWorld)
  312.     {
  313.         LockPixels(GetGWorldPixMap(theGWorld));
  314.         CopyBits(&((GrafPtr)theGWorld)->portBits, &simpleWindow->portBits, 
  315.                 &srcRect, &destRect, srcCopy, 0L);
  316.         UnlockPixels(GetGWorldPixMap(theGWorld));
  317.     }
  318.     else                
  319.         PaintRect(&destRect);
  320.     
  321.     InsetRect(&destRect, -2, -2);
  322.     FrameRect(&destRect);
  323. }
  324.  
  325. //--------------------------------------------------------------------- DoAKeyDownEvent
  326.  
  327. void DoAKeyDownEvent (EventRecord *theEvent)
  328. {
  329.     long        menuAndItem;
  330.     char        theChar;
  331.     Boolean        commandDown;
  332.     
  333.     theChar = theEvent->message & charCodeMask;
  334.     commandDown = ((theEvent->modifiers & cmdKey) != 0);
  335.     if (commandDown)
  336.     {
  337.         menuAndItem = MenuKey(theChar);
  338.         DoMenuChoice(menuAndItem);
  339.     }
  340. }
  341.  
  342. //--------------------------------------------------------------------- DoActivateEvent
  343.  
  344. void DoActivateEvent (EventRecord *theEvent)
  345. {
  346.     if (theEvent->modifiers & activeFlag)
  347.         SelectWindow((WindowPtr)theEvent->message);
  348. }
  349.  
  350. //--------------------------------------------------------------------- DoUpdateEvent
  351.  
  352. Boolean DoUpdateEvent (EventRecord *theEvent)
  353. {
  354.     GrafPtr        wasPort;
  355.     
  356.     if ((WindowPtr)theEvent->message == simpleWindow)
  357.     {
  358.         GetPort(&wasPort);
  359.         SetPort(simpleWindow);
  360.         
  361.         BeginUpdate(simpleWindow);
  362.         UpdateMainWindowText();
  363.         UpdateVennColors();
  364.         UpdateCMYKColors();
  365.         EndUpdate(simpleWindow);
  366.         
  367.         SetPort(wasPort);
  368.         return (true);
  369.     }
  370.     else
  371.         return (false);
  372. }
  373.  
  374. //--------------------------------------------------------------------- GetProfileRef
  375.  
  376. OSErr GetProfileRef (CMProfileRef *destProfile)
  377. {
  378.     CMProfileLocation    destLocation;
  379.     OSErr                theErr;
  380.     Boolean                sfGood;
  381.     
  382.     theErr = StandardGetProfile(&destLocation, &sfGood, cmRGBData);
  383.     if (sfGood)
  384.         theErr = CMOpenProfile(destProfile, &destLocation);
  385.     else
  386.         theErr = 1;
  387.     
  388.     return theErr;
  389. }
  390.  
  391. //--------------------------------------------------------------  DoAppleMenu
  392. // Handle the Apple menu (About box and desk accessories).
  393.  
  394. Boolean DoAppleMenu (short theItem)
  395. {
  396.     Str255        daName;
  397.     GrafPtr        wasPort;
  398.     short        daNumber;
  399.     Boolean            handledIt;
  400.     
  401.     handledIt = false;
  402.     
  403.     switch (theItem)
  404.     {
  405.         case 1:
  406.         break;
  407.         
  408.         default:
  409.         GetMenuItemText(GetMenuHandle(128), theItem, daName);
  410.         GetPort(&wasPort);
  411.         daNumber = OpenDeskAcc(daName);
  412.         SetPort(wasPort);
  413.         handledIt = true;
  414.         break;
  415.     }
  416.     
  417.     return handledIt;
  418. }
  419.  
  420. //--------------------------------------------------------------  DoPickerMenu
  421. // Handle the Picker menu.
  422.  
  423. Boolean DoPickerMenu (short theItem)
  424. {
  425.     CMProfileRef    destProfile, inOutProfile;
  426.     OSErr            theErr;
  427.     Boolean            handledIt;
  428.     
  429.     handledIt = false;
  430.     
  431.     switch (theItem)
  432.     {
  433.         case 1:    // GetColor().
  434.         SimpleGetColor();
  435.         handledIt = true;
  436.         break;
  437.         
  438.         case 2:    // PickColor().
  439.         PickColorMovableModal(0L);
  440.         handledIt = true;
  441.         break;
  442.         
  443.         case 3:    // HTML Picker Only.
  444.         PickColorMovableModal('HTML');
  445.         handledIt = true;
  446.         break;
  447.         
  448.         case 4:    // NPickColor().
  449.         NPickColorMovableModal();
  450.         handledIt = true;
  451.         break;
  452.         
  453.         case 5:    // CMYK Color Space.
  454.         NPickColorCMYK();
  455.         handledIt = true;
  456.         break;
  457.     }
  458.     
  459.     return handledIt;
  460. }
  461.  
  462. //--------------------------------------------------------------------- DoMenuChoice
  463.  
  464. Boolean DoMenuChoice (long menuChoice)
  465. {
  466.     short        theMenu, theItem;
  467.     Boolean        handledIt;
  468.     
  469.     handledIt = false;
  470.     if (menuChoice == 0)
  471.         return;
  472.     
  473.     theMenu = HiWord(menuChoice);
  474.     theItem = LoWord(menuChoice);
  475.     
  476.     switch (theMenu)
  477.     {
  478.         case 128:    // Apple menu.
  479.         handledIt = DoAppleMenu(theItem);
  480.         break;
  481.         
  482.         case 129:    // File menu.
  483.         if (theItem == 1)
  484.         {
  485.             quitting = true;
  486.             handledIt = true;
  487.         }
  488.         break;
  489.         
  490.         case 130:    // Edit menu.
  491.         break;
  492.         
  493.         case 131:    // Demo menu.
  494.         handledIt = DoPickerMenu(theItem);
  495.         break;
  496.     }
  497.     
  498.     HiliteMenu(0);
  499.     
  500.     return handledIt;
  501. }
  502.  
  503. //--------------------------------------------------------------------- EventLoop
  504.  
  505. void EventLoop (void)
  506. {
  507.     EventRecord    theEvent;
  508.     WindowPtr    whichWindow;
  509.     long        menuChoice;
  510.     short        thePart;
  511.     Boolean        whoCares;
  512.     
  513.     if (WaitNextEvent(everyEvent, &theEvent, 1L, nil))
  514.     {
  515.         switch (theEvent.what)
  516.         {
  517.             case mouseDown:
  518.             thePart = FindWindow(theEvent.where, &whichWindow);
  519.             switch (thePart)
  520.             {
  521.                 case inSysWindow:
  522.                 SystemClick(&theEvent, whichWindow);
  523.                 break;
  524.                 
  525.                 case inMenuBar:
  526.                 menuChoice = MenuSelect(theEvent.where);
  527.                 DoMenuChoice(menuChoice);
  528.                 break;
  529.                 
  530.                 case inDrag:
  531.                 DragWindow(whichWindow, theEvent.where, &((*LMGetGrayRgn())->rgnBBox));
  532.                 break;
  533.             }
  534.             break;
  535.             
  536.             case keyDown:
  537.             DoAKeyDownEvent(&theEvent);
  538.             break;
  539.             
  540.             case activateEvt:
  541.             DoActivateEvent(&theEvent);
  542.             break;
  543.             
  544.             case updateEvt:
  545.             whoCares = DoUpdateEvent(&theEvent);
  546.             break;
  547.         }
  548.     }
  549.     
  550.     UpdateBouncingLine();
  551. }
  552.  
  553.